<template>
  <!-- 授权用户 -->
  <el-dialog title="选择用户" :visible.sync="visible" width="800px" top="5vh" append-to-body
             :close-on-click-modal="false">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="会员名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入会员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="精确用户名称" prop="exact_name">
        <el-input
          v-model="queryParams.exact_name"
          placeholder="请输入精确用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="userList" @selection-change="handleSelectionChange"
                height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="会员id" align="center" key="id" prop="id"
                         :show-overflow-tooltip="true" width="80"
        />
        <el-table-column label="会员名称" align="center" key="name" prop="name"
                         :show-overflow-tooltip="true" width="160"
        />
        <el-table-column label="会员手机号" align="center" key="phone" prop="phone"
                         :show-overflow-tooltip="true" width="160"
        />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column prop="quantification_status" label="量化状态">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.quantification_status"/>
          </template>
        </el-table-column>
        <el-table-column prop="withdraw_status" label="提现状态">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.withdraw_status"/>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.page_size"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectCustomer">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {listUser, sendCreate} from "@/api/customer/customerAnnouncement";

export default {
  name: "SelectCustomer",
  dicts: ['sys_normal_disable'],
  props: {
    app_id: {
      type: [Number, String]
    },
    no_app_announcement_id: {
      type: [Number, String]
    },
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      user_ids: [],
      // 总条数
      total: 0,
      // 未授权用户数据
      userList: [],
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        no_app_announcement_id: undefined,
        name: undefined,
        exact_name: undefined,
        app_id: undefined,
      }
    };
  },
  methods: {
    // 显示弹框
    show() {
      this.queryParams.no_app_announcement_id = this.no_app_announcement_id;
      this.queryParams.app_id = this.app_id;
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.user_ids = selection.map(item => item.id);
    },
    // 查询表数据
    getList() {
      listUser(this.queryParams).then(res => {
        this.userList = res.data;
        this.total = res.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      let appId = this.queryParams.app_id
      this.resetForm('queryForm')
      this.queryParams.app_id = appId
      this.handleQuery();
    },
    /** 选择发送公告会员操作 */
    handleSelectCustomer() {
      const app_announcement_id = this.queryParams.no_app_announcement_id;
      const user_ids = this.user_ids.join(",");
      if (user_ids == "") {
        this.$modal.msgError("请选择要发送公告的会员");
        return;
      }
      sendCreate({app_announcement_id: app_announcement_id, customer_ids: user_ids}).then(res => {
        this.$modal.msgSuccess(res.msg);
        if (res.result_code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      });
    }
  }
};
</script>
